﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_BoxConfirmSheet : DockContent
    {
        private DGBY.BLL.JZXQRTab m_bllJZXQRTab = new DGBY.BLL.JZXQRTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.PaiCheTab m_bllPaiCheTab = new DGBY.BLL.PaiCheTab();
        private DGBY.BLL.JZXQRMXTab m_bllJZXQRMXTab = new DGBY.BLL.JZXQRMXTab();
        private DGBY.BLL.PortTab m_bllPortTab = new DGBY.BLL.PortTab();
        private DGBY.BLL.PaiCheZGMXTab m_bllPaiCheZGMXTab = new DGBY.BLL.PaiCheZGMXTab();
        private DGBY.BLL.GuiXingTab m_bllGuiXingTab = new DGBY.BLL.GuiXingTab();
        private int m_nSelectJZXQRTabID = 0;
        private int m_nSelectJZXQRMXTabID = 0;


        public Frm_BoxConfirmSheet()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            DataSet ds = m_bllPortTab.GetList("");
            DataSet ds1 = m_bllPortTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGetBoxPlace.DataSource = ds.Tables[0];
                this.comboBoxGetBoxPlace.DisplayMember = "港口名称";
                this.comboBoxGetBoxPlace.ValueMember = "PortTabID";
                this.comboBoxGetBoxPlace.SelectedIndex = 0;

                this.comboBoxBoxComePlace.DataSource = ds1.Tables[0];
                this.comboBoxBoxComePlace.DisplayMember = "港口名称";
                this.comboBoxBoxComePlace.ValueMember = "PortTabID";
                this.comboBoxBoxComePlace.SelectedIndex = 0;
            }
            ds = m_bllGuiXingTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGuiXing.DataSource = ds.Tables[0];
                this.comboBoxGuiXing.DisplayMember = "柜型";
                this.comboBoxGuiXing.ValueMember = "GuiXingTabID";
                this.comboBoxGuiXing.SelectedIndex = 0;
            }
            ds = m_bllPaiCheTab.ShowAllPCDRecByHTCode("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDjNo.DataSource = ds.Tables[0];
                this.comboBoxDjNo.ValueMember = "PaiCheTabID";
                this.comboBoxDjNo.DisplayMember = "单据号";
                this.comboBoxDjNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxDjNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxDjNo.SelectedIndex = 0;
            }
        }
        private void Frm_BoxConfirmSheet_Load(object sender, EventArgs e)
        {
           // this.groupBoxBoxConfirmDetail.Visible = false;

            InitCombox();


        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxDjNo.Text.ToString();
                int nID = 0;

                DataSet ds = m_bllPaiCheTab.GetList("PaiCheDJNo = '" + this.comboBoxDjNo.Text + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaiCheTabID"].ToString(), 10);
                }

                ShowJZXQRSheetByContractCode(nID);

                DataSet ds2 = m_bllJZXQRTab.GetDispatchCarInfo("PaiCheTab.PaiCheTabID = '" + nID + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    this.textBoxSelfComp.Text = ds2.Tables[0].Rows[0]["CompanyName"].ToString();
                    this.textBoxFacName.Text = ds2.Tables[0].Rows[0]["FactoryName"].ToString();
                    this.textBoxTrailerCompName.Text = ds2.Tables[0].Rows[0]["TCCompName"].ToString();
                }
            }
            catch { }

        }


        private void ShowJZXQRSheetByContractCode(int nID)
        {

            DataSet m_ds = m_bllJZXQRTab.ShowQRD("PaiCheTab.PaiCheTabID = '" + nID + "'");
            ultraGrid1.DataSource = null;

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
        }

        private void ShowAllRecordInDataGridView(string strWhere)
        {

            DataSet m_ds = m_bllJZXQRTab.ShowQRD(strWhere);
            ultraGrid1.DataSource = null;

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
        }

        private void buttonShowAllRec_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }


        private void cellJZXQRTabClick()
        {
            DGBY.Model.JZXQRTab modelJZXQRTab = m_bllJZXQRTab.GetModel(m_nSelectJZXQRTabID);
            if (modelJZXQRTab != null)
            {
                
                this.textBoxPreserveNo.Text = modelJZXQRTab.DingDanNo;
               // this.textBoxThisSheetNo.Text = modelJZXQRTab.JZXQRDJNo.ToString();
                this.textBoxNote.Text = modelJZXQRTab.Note;
                string[] sArray = new string[2];
              

                this.dateTimePickerArriveFacDate.Value = (System.DateTime)modelJZXQRTab.ArriveFacDate;
                sArray = modelJZXQRTab.ArriveFacTime.Split(':');
                this.textBoxArriveFacDateHour.Text = sArray[0];
                this.textBoxArriveFacDateMin.Text = sArray[1];

                this.dateTimePickerZG.Value = (System.DateTime)modelJZXQRTab.TiGuiDate;
                sArray = modelJZXQRTab.TiGuiTime.Split(':');
                this.textBoxZGHour.Text = sArray[0];
                this.textBoxZGMin.Text = sArray[1];


                this.dateTimePickerLeaveFacDate.Value = (System.DateTime)modelJZXQRTab.GoFacDate;
  
                this.textBoxLeaveFacDateMin.Text = modelJZXQRTab.GoFacTime;


                if (modelJZXQRTab.JGPosTabID != null)
                    this.comboBoxBoxComePlace.SelectedValue = modelJZXQRTab.JGPosTabID;
                if (modelJZXQRTab.PortTabID != null)
                    this.comboBoxGetBoxPlace.SelectedValue = modelJZXQRTab.PortTabID;
                if (modelJZXQRTab.GuiXingTabID != null)
                    this.comboBoxGuiXing.SelectedValue = modelJZXQRTab.GuiXingTabID;
                if (modelJZXQRTab.PaiCheTabID != null)
                    this.comboBoxDjNo.SelectedValue = modelJZXQRTab.PaiCheTabID;
                //if (modelJZXQRTab.PaidanRegisterTabID != null)
                //    this.comboBoxContractNo.SelectedValue = modelJZXQRTab.PaidanRegisterTabID;


                this.textBoxBoxNo.Text = modelJZXQRTab.GuiNo;
                this.textBoxClosureNo.Text = modelJZXQRTab.FengTiaoNo;
                this.textBoxDriverName.Text = modelJZXQRTab.SJName;
                this.textBoxDriverPhone.Text = modelJZXQRTab.SJMobile;
                this.textBoxCarMarkNo.Text = modelJZXQRTab.CarPaiNo;
                this.textBoxBookCabinNo.Text = modelJZXQRTab.DingCangNo;
                this.textBoxGBWeight.Text = modelJZXQRTab.GuoBangWeight.ToString();


            }

        }



        private Int64 MakeSJPackDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllJZXQRTab.GetSJPackDJNoByDate("JZXQRDJDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }
 
        private void buttonAddBoxConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.JZXQRTab modelJZXQRTab = new DGBY.Model.JZXQRTab();


                modelJZXQRTab.JZXQRDJNo = MakeSJPackDJNo();
                modelJZXQRTab.JZXQRDJDate = System.DateTime.Now.Date;

                modelJZXQRTab.DingDanNo = this.textBoxPreserveNo.Text;
                modelJZXQRTab.Note = this.textBoxNote.Text;


                modelJZXQRTab.ArriveFacDate = this.dateTimePickerArriveFacDate.Value;
                modelJZXQRTab.GoFacDate = this.dateTimePickerLeaveFacDate.Value;
                modelJZXQRTab.TiGuiDate = this.dateTimePickerZG.Value;

                modelJZXQRTab.TiGuiTime = this.textBoxZGHour.Text + ":" + this.textBoxZGMin.Text;
                modelJZXQRTab.ArriveFacTime = this.textBoxArriveFacDateHour.Text + ":" + this.textBoxArriveFacDateMin.Text;
                modelJZXQRTab.GoFacTime = this.textBoxLeaveFacDateMin.Text;

                modelJZXQRTab.DingCangNo = this.textBoxBookCabinNo.Text;
                modelJZXQRTab.GuiNo = this.textBoxBoxNo.Text;
                modelJZXQRTab.FengTiaoNo = this.textBoxClosureNo.Text;
                modelJZXQRTab.SJName = this.textBoxDriverName.Text;
                modelJZXQRTab.SJMobile = this.textBoxDriverPhone.Text;
                modelJZXQRTab.CarPaiNo = this.textBoxCarMarkNo.Text;

                modelJZXQRTab.GuoBangWeight = Convert.ToInt32(this.textBoxGBWeight.Text, 10);

          
          
                DataSet ds = m_bllPaiCheTab.GetList("PaiCheDJNo = '" + this.comboBoxDjNo.Text + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    modelJZXQRTab.PaiCheTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaiCheTabID"].ToString(), 10);
                }

           
                if (this.comboBoxGetBoxPlace.SelectedValue != null)
                    modelJZXQRTab.PortTabID = Convert.ToInt32(this.comboBoxGetBoxPlace.SelectedValue.ToString(), 10);
                if (this.comboBoxBoxComePlace.SelectedValue != null)
                    modelJZXQRTab.JGPosTabID = Convert.ToInt32(this.comboBoxBoxComePlace.SelectedValue.ToString(), 10);
                if (this.comboBoxGuiXing.SelectedValue != null)
                    modelJZXQRTab.GuiXingTabID = Convert.ToInt32(this.comboBoxGuiXing.SelectedValue.ToString(), 10);

                m_bllJZXQRTab.Add(modelJZXQRTab);
                ShowAllRecordInDataGridView("PaiCheTab.PaiCheTabID = '" + modelJZXQRTab.PaiCheTabID + "'");
              //  ShowAllRecordInDataGridView("PaidanRegisterTab.PaidanRegisterTabID = '" + modelJZXQRTab.PaidanRegisterTabID + "'");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        
        }

        private void buttonModifyBoxConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.JZXQRTab modelJZXQRTab = m_bllJZXQRTab.GetModel(m_nSelectJZXQRTabID);
                if (modelJZXQRTab == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
               // DGBY.Model.JZXQRTab modelJZXQRTab = new DGBY.Model.JZXQRTab();

              //  modelJZXQRTab.JZXQRDJNo = MakeSJPackDJNo();
              //  modelJZXQRTab.JZXQRDJDate = System.DateTime.Now.Date;

                modelJZXQRTab.DingDanNo = this.textBoxPreserveNo.Text;
                modelJZXQRTab.Note = this.textBoxNote.Text;


                modelJZXQRTab.ArriveFacDate = this.dateTimePickerArriveFacDate.Value;
                modelJZXQRTab.GoFacDate = this.dateTimePickerLeaveFacDate.Value;
                modelJZXQRTab.TiGuiDate = this.dateTimePickerZG.Value;

                modelJZXQRTab.TiGuiTime = this.textBoxZGHour.Text + ":" + this.textBoxZGMin.Text;
                modelJZXQRTab.ArriveFacTime = this.textBoxArriveFacDateHour.Text + ":" + this.textBoxArriveFacDateMin.Text;
                modelJZXQRTab.GoFacTime =  this.textBoxLeaveFacDateMin.Text;

                modelJZXQRTab.DingCangNo = this.textBoxBookCabinNo.Text;
                modelJZXQRTab.GuiNo = this.textBoxBoxNo.Text;
                modelJZXQRTab.FengTiaoNo = this.textBoxClosureNo.Text;
                modelJZXQRTab.SJName = this.textBoxDriverName.Text;
                modelJZXQRTab.SJMobile = this.textBoxDriverPhone.Text;
                modelJZXQRTab.CarPaiNo = this.textBoxCarMarkNo.Text;

                modelJZXQRTab.GuoBangWeight = Convert.ToInt32(this.textBoxGBWeight.Text, 10);

                DataSet ds = m_bllPaiCheTab.GetList("PaiCheDJNo = '" + this.comboBoxDjNo.Text + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    modelJZXQRTab.PaiCheTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaiCheTabID"].ToString(), 10);
                }
                if (this.comboBoxGetBoxPlace.SelectedValue != null)
                    modelJZXQRTab.PortTabID = Convert.ToInt32(this.comboBoxGetBoxPlace.SelectedValue.ToString(), 10);
                if (this.comboBoxBoxComePlace.SelectedValue != null)
                    modelJZXQRTab.JGPosTabID = Convert.ToInt32(this.comboBoxBoxComePlace.SelectedValue.ToString(), 10);
                if (this.comboBoxGuiXing.SelectedValue != null)
                    modelJZXQRTab.GuiXingTabID = Convert.ToInt32(this.comboBoxGuiXing.SelectedValue.ToString(), 10);
                m_bllJZXQRTab.Update(modelJZXQRTab);

                ShowAllRecordInDataGridView("PaiCheTab.PaiCheTabID = '" + modelJZXQRTab.PaiCheTabID + "'");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            
           
        }

        private void buttonDelBoxConfirm_Click(object sender, EventArgs e)
        {
            DGBY.Model.JZXQRTab modelJZXQRTabOld = m_bllJZXQRTab.GetModel(m_nSelectJZXQRTabID);
            if (modelJZXQRTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllJZXQRTab.Delete(m_nSelectJZXQRTabID);

                this.textBoxPreserveNo.Text = "";
                //this.textBoxThisSheetNo.Text = "";
                this.textBoxNote.Text = "";









                this.textBoxArriveFacDateHour.Text = "";
                this.textBoxArriveFacDateMin.Text = "";



                this.textBoxLeaveFacDateMin.Text = "";
                ShowAllRecordInDataGridView("PaiCheTab.PaiCheTabID = '" + modelJZXQRTabOld.PaiCheTabID + "'");
           //     ShowAllRecordInDataGridView("PaidanRegisterTab.PaidanRegisterTabID = '" + modelJZXQRTabOld.PaidanRegisterTabID + "'");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void cellJZXQRMXTabClick()
        {
            DGBY.Model.JZXQRMXTab modelJZXQRMXTab = m_bllJZXQRMXTab.GetModel(m_nSelectJZXQRMXTabID);
            if (modelJZXQRMXTab != null)
            {
                DataSet ds2 = m_bllPaiCheZGMXTab.GetList("PaiCheZGMXTabID = '" + modelJZXQRMXTab.PaiCheZGMXTabID + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                 //   this.comboBoxDisCarDetailItemNo.SelectedValue = ds2.Tables[0].Rows[0]["ItmeNo"].ToString();
                    
                }

                if (modelJZXQRMXTab.JGPosTabID != null)
                    this.comboBoxBoxComePlace.SelectedValue = modelJZXQRMXTab.JGPosTabID;

                this.textBoxBoxNo.Text = modelJZXQRMXTab.GuiNo;
                this.textBoxClosureNo.Text = modelJZXQRMXTab.FengTiaoNo;
                this.textBoxDriverName.Text = modelJZXQRMXTab.SJName;
                this.textBoxDriverPhone.Text = modelJZXQRMXTab.SJMobile;
                this.textBoxCarMarkNo.Text = modelJZXQRMXTab.CarPaiNo;
            }
        }

       

      
     
        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.LocalPrintPreveiw();
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strJZXQRTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectJZXQRTabID = Convert.ToInt32(strJZXQRTabID, 10);
            cellJZXQRTabClick();
        }

        private void buttonExport2Excel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;

            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void Frm_BoxConfirmSheet_Activated(object sender, EventArgs e)
        {
            InitCombox();
        }

        private void Frm_BoxConfirmSheet_Deactivate(object sender, EventArgs e)
        {
           // Frm_Main.m_nContractSelectIndex = this.comboBoxDjNo.SelectedIndex;
        }
     
       


    }
}